package com.ncs.cns.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.ncs.cns.R;
import com.ncs.cns.business.service.LoginService;
import com.ncs.cns.business.to.UserNcs;
import com.ncs.cns.business.util.StringUtils;
import com.ncs.cns.framework.Constant;

public class RegisterActivity extends BaseActivity {
	
	private EditText userId = null;
	private EditText userPwd = null;
	private EditText userPwd2 = null;
	private EditText email;
	private ImageButton mClear1;
	private ImageButton mClear2;
	private ImageButton mClear3;
	private ImageButton mClear4;
	
	private Button registerBtn = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		userId = (EditText)this.findViewById(R.id.userId);
		userPwd = (EditText)this.findViewById(R.id.userPwd);
		userPwd2 = (EditText)this.findViewById(R.id.userPwd2);
		email = (EditText)this.findViewById(R.id.email);
		
		registerBtn = (Button)this.findViewById(R.id.registerBtn);
		mClear1 = (ImageButton)findViewById(R.id.clearUserId);
		mClear2 = (ImageButton)findViewById(R.id.clearPwd);
		mClear3 = (ImageButton)findViewById(R.id.clearPwd2);
		mClear4 = (ImageButton)findViewById(R.id.clearEmail);
		
		mClear1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userId.setText("");
			}
		});
		
		mClear2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userPwd.setText("");
			}
		});
		
		mClear3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userPwd2.setText("");
			}
		});
		
		mClear4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				email.setText("");
			}
		});
		
		setUserIdTextChangeListener(userId, mClear1);
        setUserPwdTextChangeListener(userPwd, mClear2);
        setUserPwdTextChangeListener(userPwd2, mClear3);
        setEmailTextChangeListener();
        registerBtn.setOnClickListener(new RegisterOnClickListener());
		
	}
	
	private void setEmailTextChangeListener(){
		email.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String str = email.getText().toString().trim();
				int length = str.length();
				if (length > 0) {
					mClear4.setVisibility(View.VISIBLE);
					if(StringUtils.isContainChinese(str)){
						str = str.substring(0, length-1);
						email.setText(str);
						String str1 = email.getText().toString().trim();
						email.setSelection(str1.length());
						showToast(R.string.error_email_expr2);
					}
					mClear4.postDelayed(new Runnable(){

						@Override
						public void run() {
							mClear4.setVisibility(View.INVISIBLE);
						}
						
					}, 5000);
				} else {
					mClear4.setVisibility(View.INVISIBLE);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			public void afterTextChanged(Editable s) {

			}
		});
	}

	public class  RegisterOnClickListener implements View.OnClickListener{
		public void onClick(View arg0) {
			String userIdStr = userId.getText().toString();
			String password = userPwd.getText().toString();
			
			if(TextUtils.isEmpty(userIdStr)){
				showToast(R.string.error_user_id_empty);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (!StringUtils.isNumberLetter(userIdStr)) {
				showToast(R.string.error_user_id_expr);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(userIdStr)<3) {
				showToast(R.string.error_user_id_min_length);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(userIdStr)>20) {
				showToast(R.string.error_user_id_max_length);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (TextUtils.isEmpty(password)) {
				showToast(R.string.error_pwd);
				userPwd.setFocusable(true);
				userPwd.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(password)<6) {
				showToast(R.string.error_pwd_min_length);
				userPwd.setFocusable(true);
				userPwd.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(password)>20) {
				showToast(R.string.error_pwd_max_length);
				userPwd.setFocusable(true);
				userPwd.requestFocus();
				return;
			}
			
			if(!password.equals(userPwd2.getText().toString())){
				showToast(R.string.error_pwd_not_same);
				return;
			}
			
			String emailStr = email.getText().toString();
			if (StringUtils.isEmpty(emailStr)) {
				showToast(R.string.error_email);
				email.setFocusable(true);
				email.requestFocus();
				return;
			}
			
			if (!StringUtils.isEmail(emailStr)) {
				showToast(R.string.error_email_expr);
				email.setFocusable(true);
				email.requestFocus();
				return;
			}
			
			if(!commService.isNetworkAvailable()){
				showToast(R.string.error_network);
			}else{
				//判断帐号是否存在
				boolean isUserIdExist = LoginService.isUserIdExist(userIdStr);
				if(isUserIdExist){
					showToast(R.string.error_userid_exist);
				}else{
					SharedPreferences  sp = getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
					Editor editor = sp.edit();
					editor.putString(Constant.USERID_COOKIE, userIdStr);
					editor.putString(Constant.USERPASSWORD_COOKIE, password);
					editor.putBoolean(Constant.USERPASSWORDREMEMBER_COOKIE, true);
					
					editor.commit();
					UserNcs user = new UserNcs();
					user.setUserId(userIdStr);
					user.setPassword(password);
					application.setUser(user);
					
					Intent intent = new Intent(RegisterActivity.this, CircuSeeActivity.class);
					startActivity(intent);
					
				}
			}
		}        	
	}

}
